angular.module('hello', ['ngRoute'])
    .config(function ($routeProvider) {
        $routeProvider.when('/', {
            templateUrl: 'home.html',
            controller: 'home'
        }).otherwise('/');

    })
    .controller('navigation', function ($rootScope, $scope, $http, $location, $route) {
        $scope.tab = function (route) {
            return $route.current && route === $route.current.controller;
        };
        $http.get('user').success(function (data) {
            if (data.name) {
                $rootScope.authenticated = true;
            } else {
                $rootScope.authenticated = false;
            }
        }).error(function () {
            $rootScope.authenticated = false;
        });

        $scope.credentials = {}; // not used

        $scope.logout = function () {
            $http.post('logout', {}).success(function () {
                $rootScope.authenticated = false;
                $location.path("/");
            }).error(function (data) {
                console.log("Logout failed")
                $rootScope.authenticated = false;
            });
        }

    })
    .controller('home', function ($scope, $http) {
        $http.get('resource/hi').success(function (data) {
            $scope.greeting = data;
            console.log ( JSON.stringify( data));
        });

    });
